/**
 * Created by zhangwx on 2015/11/16.
 */
var map;
var source;
var  heatMap;
var  pointsMap;
var pointStyle;
var popup;
var element;
var listenterKey;
var select;

$(function() {

    var src=getRootPath()+"/static/js/mapAnalysis/olMap/style/image/marker_red_sprite.png";
    pointStyle=new ol.style.Style({

        image:new ol.style.Icon({src:src})
    });


 /*   var translate = new ol.interaction.Translate({
        features: select.getFeatures()
    });*/
    var mousePositionControl = new ol.control.MousePosition({
        coordinateFormat: ol.coordinate.createStringXY(4),
        projection: 'EPSG:4326',
        // comment the following two lines to have the mouse position
        // be placed within the map.
        //  className: 'custom-mouse-position',
        // target: document.getElementById('mouse-position'),
        undefinedHTML: '&nbsp;'
    });
    map = new ol.Map({
        layers: [
            new ol.layer.Tile({
                source: new ol.source.OSM()
            })
        ],
        target: 'map',
        controls: ol.control.defaults({
            attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
                collapsible: false
            })
        })
            .extend([
                mousePositionControl
            ]) ,
        //        projection: "EPSG:900913",


        view: new ol.View({
            //	projection: 'EPSG:4326',
            center: ol.proj.transform([116.41,39.91], 'EPSG:4326', 'EPSG:3857'),
            zoom: 13
        })
    });
   source = new ol.source.Vector({
        wrapX: false
    });


    heatMap = new ol.layer.Heatmap({
        source: source,
        blur:70 ,
        radius:50,
        opacity:0.7
    });
    map.addLayer(heatMap);
     pointsMap = new ol.layer.Vector({
     source:source
     });
     map.addLayer(pointsMap);

    // Popup showing the position the user clicked
     popup = new ol.Overlay({
        element: document.getElementById('title')
    });
    map.addOverlay(popup);
     element = popup.getElement();

});


//js获取项目根路径，如： http://localhost:8083/uimcardprj
function getRootPath(){
    //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
    var curWwwPath=window.document.location.href;
    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
    var pathName=window.document.location.pathname;
    var pos=curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8083
    var localhostPaht=curWwwPath.substring(0,pos);
    //获取带"/"的项目名，如：/uimcardprj
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    return(localhostPaht+projectName);
}

//=====================================添加特效=====================================



function addRandomFeature() {
    //var x = Math.random() * 360 - 180;
    //var y = Math.random() * 180 - 90;
    var x =73+ Math.random() * 63;
    var y =3+ Math.random() * 51;
    var geom = new ol.geom.Point(ol.proj.transform([x, y],
        'EPSG:4326', 'EPSG:3857'));
    var feature = new ol.Feature(geom);
    source.addFeature(feature);
}
/*
var duration = 3000;
function flash(feature) {
    var start = new Date().getTime();
    var listenerKey;

    function animate(event) {
        var vectorContext = event.vectorContext;
        var frameState = event.frameState;
        var flashGeom = feature.getGeometry().clone();
        var elapsed = frameState.time - start;
        var elapsedRatio = elapsed / duration;
        // radius will be 5 at start and 30 at end.
        var radius = ol.easing.easeOut(elapsedRatio) * 25 + 5;
        var opacity = ol.easing.easeOut(1 - elapsedRatio);

        var flashStyle = new ol.style.Circle({
            radius: radius,
            snapToPixel: false,
            stroke: new ol.style.Stroke({
                color: 'rgba(255, 0, 0, ' + opacity + ')',
                width: 1
            })
        });

        vectorContext.setImageStyle(flashStyle);
        vectorContext.drawPointGeometry(flashGeom, null);
        if (elapsed > duration) {
            ol.Observable.unByKey(listenerKey);
            return;
        }
        // tell OL3 to continue postcompose animation
        frameState.animate = true;
    }
    listenerKey = map.on('postcompose', animate);
}
*/




/*window.setInterval(addRandomFeature, 1000);*/

//=====================================添加特效=====================================